package com.huawei.petapp2.address.edit;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.huawei.petapp2.R;
import com.huawei.petapp2.activity.widget.BaseActivity;
import com.huawei.petapp2.activity.widget.CommonPopWindow;
import com.huawei.petapp2.activity.widget.wrapper.ToastWrapper;
import com.huawei.petapp2.address.manage.ManageAddressActivity;
import com.huawei.petapp2.addressselector.AddressSelector;
import com.huawei.petapp2.addressselector.CityInterface;
import com.huawei.petapp2.addressselector.OnItemClickListener;
import com.huawei.petapp2.constant.HttpUrl;
import com.huawei.petapp2.entity.Address;
import com.huawei.petapp2.entity.AddressSelectorReq;
import com.huawei.petapp2.entity.ItemAddressReq;
import com.huawei.petapp2.entity.Result;
import com.huawei.petapp2.network.UserManager;
import com.huawei.petapp2.network.api.ApiRegion;
import com.huawei.petapp2.network.core.ApiPath;
import com.huawei.petapp2.network.core.ResponseEntity;
import com.huawei.petapp2.utils.PreferenceHelper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 新增/编辑 地址界面.
 */
public class EditAddressActivity extends BaseActivity implements View.OnClickListener,CommonPopWindow.ViewClickListener{

    private static final String EXTRA_ADDRESS = "EXTRA_ADDRESS";

    /**
     * @param address 非空时是编辑地址, 否则是创建新地址.
     */
    public static Intent getStartIntent(Context context, @Nullable Address address) {
        Intent intent = new Intent(context, EditAddressActivity.class);
        intent.putExtra(EXTRA_ADDRESS, new Gson().toJson(address));
        return intent;
    }

    @BindView(R.id.edit_consignee)
    EditText etConsignee;
    @BindView(R.id.edit_tel)
    EditText etTel;
    @BindView(R.id.edit_detail)
    EditText etDetail;
    @BindView(R.id.edit_zipcode)
    EditText etZipcode;
    @BindView(R.id.edit_email)
    EditText etEmail;
    @BindView(R.id.button_save)
    Button btnSave;
    @BindView(R.id.text_region)
    TextView tvRegion;

    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.standard_order_title)
    TextView TextView;

    @SuppressLint("NonConstantResourceId")
    @BindView(R.id.standard_toolbar_back)
    ImageView imageView;
    private String mConsignee; // 收件人姓名
    private String mTel; // 手机号码
    private String mDetail; // 详细地址
    private String mZipCode; // 邮编
    private String mEmail; // 邮箱

    private String mProvinceId; // 省份Id
    private String mProvinceName; // 省份名称

    private String mCityId; // 城市Id
    private String mCityName; // 城市名称

    private String mDistrictId; // 地区Id
    private String mDistrictName; // 地区名称
    private Address mAddress;

    private View v;
    private Button btn;
    private TextView text_suozaidiqu;
    private Toolbar toolbar ;
    String token=PreferenceHelper.getString("token",Objects.requireNonNull(getContext()));
    final private static Gson gson=new GsonBuilder().serializeNulls().create();
    private String response ="{\n" +
            "    \"datas\": [\n" +
            "        {\n" +
            "            \"areaAbbName\": \"北京\",\n" +
            "            \"areaCode\": \"110000\",\n" +
            "            \"areaEnName\": \"BEI JING\",\n" +
            "            \"areaName\": \"北京\",\n" +
            "            \"areaType\": \"1\",\n" +
            "            \"areaZip\": \"100000\",\n" +
            "            \"children\": [\n" +
            "                {\n" +
            "                    \"areaAbbName\": \"北京\",\n" +
            "                    \"areaCode\": \"110100\",\n" +
            "                    \"areaEnName\": \"BEI JING\",\n" +
            "                    \"areaName\": \"北京市\",\n" +
            "                    \"areaType\": \"2\",\n" +
            "                    \"areaZip\": \"100000\",\n" +
            "                    \"children\": [\n" +
            "                        {\n" +
            "                            \"areaAbbName\": \"东城\",\n" +
            "                            \"areaCode\": \"110101\",\n" +
            "                            \"areaEnName\": \"DONG CHENG DISTRICT\",\n" +
            "                            \"areaName\": \"东城区\",\n" +
            "                            \"areaType\": \"3\",\n" +
            "                            \"areaZip\": \"100000\",\n" +
            "                            \"children\": [],\n" +
            "                            \"id\": \"4\",\n" +
            "                            \"parentId\": \"3\"\n" +
            "                        },\n" +
            "                        {\n" +
            "                            \"areaAbbName\": \"西城\",\n" +
            "                            \"areaCode\": \"110102\",\n" +
            "                            \"areaEnName\": \"XI CHENG DISTRICT\",\n" +
            "                            \"areaName\": \"西城区\",\n" +
            "                            \"areaType\": \"3\",\n" +
            "                            \"areaZip\": \"100000\",\n" +
            "                            \"children\": [],\n" +
            "                            \"id\": \"5\",\n" +
            "                            \"parentId\": \"3\"\n" +
            "                        },\n" +
            "                        {\n" +
            "                            \"areaAbbName\": \"崇文\",\n" +
            "                            \"areaCode\": \"110103\",\n" +
            "                            \"areaEnName\": \"CHONG WEN DISTRICT\",\n" +
            "                            \"areaName\": \"崇文区\",\n" +
            "                            \"areaType\": \"3\",\n" +
            "                            \"areaZip\": \"100000\",\n" +
            "                            \"children\": [],\n" +
            "                            \"id\": \"6\",\n" +
            "                            \"parentId\": \"3\"\n" +
            "                        },\n" +
            "                        {\n" +
            "                            \"areaAbbName\": \"宣武\",\n" +
            "                            \"areaCode\": \"110104\",\n" +
            "                            \"areaEnName\": \"XUAN WU DISTRICT\",\n" +
            "                            \"areaName\": \"宣武区\",\n" +
            "                            \"areaType\": \"3\",\n" +
            "                            \"areaZip\": \"100000\",\n" +
            "                            \"children\": [],\n" +
            "                            \"id\": \"7\",\n" +
            "                            \"parentId\": \"3\"\n" +
            "                        },\n" +
            "                        {\n" +
            "                            \"areaAbbName\": \"朝阳\",\n" +
            "                            \"areaCode\": \"110105\",\n" +
            "                            \"areaEnName\": \"CHAO YANG DISTRICT\",\n" +
            "                            \"areaName\": \"朝阳区\",\n" +
            "                            \"areaType\": \"3\",\n" +
            "                            \"areaZip\": \"100000\",\n" +
            "                            \"children\": [],\n" +
            "                            \"id\": \"8\",\n" +
            "                            \"parentId\": \"3\"\n" +
            "                        },\n" +
            "                        {\n" +
            "                            \"areaAbbName\": \"丰台\",\n" +
            "                            \"areaCode\": \"110106\",\n" +
            "                            \"areaEnName\": \"FENG TAI DISTRICT\",\n" +
            "                            \"areaName\": \"丰台区\",\n" +
            "                            \"areaType\": \"3\",\n" +
            "                            \"areaZip\": \"100000\",\n" +
            "                            \"children\": [],\n" +
            "                            \"id\": \"9\",\n" +
            "                            \"parentId\": \"3\"\n" +
            "                        },\n" +
            "                        {\n" +
            "                            \"areaAbbName\": \"石景山\",\n" +
            "                            \"areaCode\": \"110107\",\n" +
            "                            \"areaEnName\": \"SHI JING SHAN DISTRICT\",\n" +
            "                            \"areaName\": \"石景山区\",\n" +
            "                            \"areaType\": \"3\",\n" +
            "                            \"areaZip\": \"100000\",\n" +
            "                            \"children\": [],\n" +
            "                            \"id\": \"10\",\n" +
            "                            \"parentId\": \"3\"\n" +
            "                        },\n" +
            "                        {\n" +
            "                            \"areaAbbName\": \"海淀\",\n" +
            "                            \"areaCode\": \"110108\",\n" +
            "                            \"areaEnName\": \"HAI DIAN DISTRICT\",\n" +
            "                            \"areaName\": \"海淀区\",\n" +
            "                            \"areaType\": \"3\",\n" +
            "                            \"areaZip\": \"100000\",\n" +
            "                            \"children\": [],\n" +
            "                            \"id\": \"11\",\n" +
            "                            \"parentId\": \"3\"\n" +
            "                        },\n" +
            "                        {\n" +
            "                            \"areaAbbName\": \"门头沟\",\n" +
            "                            \"areaCode\": \"110109\",\n" +
            "                            \"areaEnName\": \"MEN TOU GOU DISTRICT\",\n" +
            "                            \"areaName\": \"门头沟区\",\n" +
            "                            \"areaType\": \"3\",\n" +
            "                            \"areaZip\": \"102300\",\n" +
            "                            \"children\": [],\n" +
            "                            \"id\": \"12\",\n" +
            "                            \"parentId\": \"3\"\n" +
            "                        },\n" +
            "                        {\n" +
            "                            \"areaAbbName\": \"房山\",\n" +
            "                            \"areaCode\": \"110111\",\n" +
            "                            \"areaEnName\": \"FANG SHAN DISTRICT\",\n" +
            "                            \"areaName\": \"房山区\",\n" +
            "                            \"areaType\": \"3\",\n" +
            "                            \"areaZip\": \"102400\",\n" +
            "                            \"children\": [],\n" +
            "                            \"id\": \"13\",\n" +
            "                            \"parentId\": \"3\"\n" +
            "                        },\n" +
            "                        {\n" +
            "                            \"areaAbbName\": \"通州\",\n" +
            "                            \"areaCode\": \"110112\",\n" +
            "                            \"areaEnName\": \"TONG ZHOU DISTRICT\",\n" +
            "                            \"areaName\": \"通州区\",\n" +
            "                            \"areaType\": \"3\",\n" +
            "                            \"areaZip\": \"101100\",\n" +
            "                            \"children\": [],\n" +
            "                            \"id\": \"14\",\n" +
            "                            \"parentId\": \"3\"\n" +
            "                        },\n" +
            "                        {\n" +
            "                            \"areaAbbName\": \"顺义\",\n" +
            "                            \"areaCode\": \"110113\",\n" +
            "                            \"areaEnName\": \"SHUN YI DISTRICT\",\n" +
            "                            \"areaName\": \"顺义区\",\n" +
            "                            \"areaType\": \"3\",\n" +
            "                            \"areaZip\": \"101300\",\n" +
            "                            \"children\": [],\n" +
            "                            \"id\": \"15\",\n" +
            "                            \"parentId\": \"3\"\n" +
            "                        },\n" +
            "                        {\n" +
            "                            \"areaAbbName\": \"昌平\",\n" +
            "                            \"areaCode\": \"110114\",\n" +
            "                            \"areaEnName\": \"CHANG PING DISTRICT\",\n" +
            "                            \"areaName\": \"昌平区\",\n" +
            "                            \"areaType\": \"3\",\n" +
            "                            \"areaZip\": \"102200\",\n" +
            "                            \"children\": [],\n" +
            "                            \"id\": \"16\",\n" +
            "                            \"parentId\": \"3\"\n" +
            "                        },\n" +
            "                        {\n" +
            "                            \"areaAbbName\": \"大兴\",\n" +
            "                            \"areaCode\": \"110115\",\n" +
            "                            \"areaEnName\": \"DA XING DISTRICT\",\n" +
            "                            \"areaName\": \"大兴区\",\n" +
            "                            \"areaType\": \"3\",\n" +
            "                            \"areaZip\": \"102600\",\n" +
            "                            \"children\": [],\n" +
            "                            \"id\": \"17\",\n" +
            "                            \"parentId\": \"3\"\n" +
            "                        },\n" +
            "                        {\n" +
            "                            \"areaAbbName\": \"怀柔\",\n" +
            "                            \"areaCode\": \"110116\",\n" +
            "                            \"areaEnName\": \"HUAI ROU DISTRICT\",\n" +
            "                            \"areaName\": \"怀柔区\",\n" +
            "                            \"areaType\": \"3\",\n" +
            "                            \"areaZip\": \"101400\",\n" +
            "                            \"children\": [],\n" +
            "                            \"id\": \"18\",\n" +
            "                            \"parentId\": \"3\"\n" +
            "                        },\n" +
            "                        {\n" +
            "                            \"areaAbbName\": \"平谷\",\n" +
            "                            \"areaCode\": \"110117\",\n" +
            "                            \"areaEnName\": \"PING GU DISTRICT\",\n" +
            "                            \"areaName\": \"平谷区\",\n" +
            "                            \"areaType\": \"3\",\n" +
            "                            \"areaZip\": \"101200\",\n" +
            "                            \"children\": [],\n" +
            "                            \"id\": \"19\",\n" +
            "                            \"parentId\": \"3\"\n" +
            "                        },\n" +
            "                        {\n" +
            "                            \"areaAbbName\": \"密云\",\n" +
            "                            \"areaCode\": \"110228\",\n" +
            "                            \"areaEnName\": \"MI YUN COUNTY\",\n" +
            "                            \"areaName\": \"密云县\",\n" +
            "                            \"areaType\": \"3\",\n" +
            "                            \"areaZip\": \"101500\",\n" +
            "                            \"children\": [],\n" +
            "                            \"id\": \"20\",\n" +
            "                            \"parentId\": \"3\"\n" +
            "                        },\n" +
            "                        {\n" +
            "                            \"areaAbbName\": \"延庆\",\n" +
            "                            \"areaCode\": \"110229\",\n" +
            "                            \"areaEnName\": \"YAN QING COUNTY\",\n" +
            "                            \"areaName\": \"延庆县\",\n" +
            "                            \"areaType\": \"3\",\n" +
            "                            \"areaZip\": \"102100\",\n" +
            "                            \"children\": [],\n" +
            "                            \"id\": \"21\",\n" +
            "                            \"parentId\": \"3\"\n" +
            "                        },\n" +
            "                        {\n" +
            "                            \"areaAbbName\": \"其它\",\n" +
            "                            \"areaCode\": \"110230\",\n" +
            "                            \"areaEnName\": \"QI TA DISTRICT\",\n" +
            "                            \"areaName\": \"其它区\",\n" +
            "                            \"areaType\": \"3\",\n" +
            "                            \"areaZip\": \"100000\",\n" +
            "                            \"children\": [],\n" +
            "                            \"id\": \"22\",\n" +
            "                            \"parentId\": \"3\"\n" +
            "                        }\n" +
            "                    ],\n" +
            "                    \"id\": \"3\",\n" +
            "                    \"parentId\": \"2\"\n" +
            "                }\n" +
            "            ],\n" +
            "            \"id\": \"2\",\n" +
            "            \"parentId\": \"1\"\n" +
            "        }\n" +
            "    ],\n" +
            "    \"msg\": \"success\",\n" +
            "    \"ret\": 0\n" +
            "}";
    private String[] saveId = new String[3];
    private List<AddressSelectorReq.DatasBean.ChildrenBeanX> childrenBeanXList;
    private List<AddressSelectorReq.DatasBean.ChildrenBeanX.ChildrenBean> childrenBeans;


    @Override
    protected int setLayoutId() {
        return R.layout.activity_edit_adress;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initEvent() {

    }

    @Override
    public void initView() {

        String str = getIntent().getStringExtra(EXTRA_ADDRESS);

        this.toolbar = findViewById(R.id.order_toolbar);
        this.toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getContext(), ManageAddressActivity.class);
                startActivity(intent);
            }
        });

        if (!TextUtils.isEmpty(str)) {
            mAddress = new Gson().fromJson(str, Address.class);
        }

        if (mAddress != null) {
            //设置收货人信息
            etConsignee.setText(mAddress.getReceiverName());
            etDetail.setText(mAddress.getAddr());
            etTel.setText(mAddress.getReceiverName());
            etZipcode.setText(mAddress.getPostCode());
            etEmail.setText("111111@qq.com");
            //设置地址信息
            mProvinceId = mAddress.getProvince();
            mProvinceName = mAddress.getProvince();
            mCityId = mAddress.getCity();
            mCityName = mAddress.getCity();
            mDistrictId = mAddress.getArea();
            mDistrictName = mAddress.getArea();
            setRegionText();
            checkAddressComplete();
        }
        v=getWindow().getDecorView();
        onClick(getWindow().getDecorView());
    }



    @Override
    protected void onBusinessResponse(String apiPath, boolean success, ResponseEntity rsp) {
        switch (apiPath) {
            case ApiPath.ADDRESS_ADD:
                UserManager.getInstance().retrieveAddressList();
                if (success) {
                    ToastWrapper.show(R.string.address_msg_add_success);
                    finish();
                }
                break;
            case ApiPath.REGION:
                if (success) {
                    ApiRegion.Rsp regionRsp = (ApiRegion.Rsp) rsp;
                } else {
                    clearRegionInfo();
                }
                break;
            case ApiPath.ADDRESS_UPDATE:
                UserManager.getInstance().retrieveAddressList();
                if (success) {
                    ToastWrapper.show(R.string.address_msg_edit_success);
                    finish();
                }
                break;
            default:
                throw new UnsupportedOperationException(apiPath);
        }
    }

    @OnTextChanged({R.id.edit_consignee, R.id.edit_tel, R.id.edit_detail, R.id.edit_zipcode, R.id.edit_email})
    void onTextChanged() {
        mConsignee = etConsignee.getText().toString();
        mTel = etTel.getText().toString();
        mDetail = etDetail.getText().toString();
        mZipCode = etZipcode.getText().toString();
        mEmail = etEmail.getText().toString();
        checkAddressComplete();
    }


    /*@OnClick(R.id.text_region) void selectRegion() {
        clearRegionInfo();
        System.out.println("你点道理11");
        // 获取省份列表.
        setAddressSelectorPopup(getWindow().getDecorView());
    }*/

    @Override
    public void onClick(View view) {
        System.out.println("你点道理");
        switch (view.getId()) {
            case R.id.text_region:
                setAddressSelectorPopup(view);
                break;
            case R.id.standard_toolbar_back:
                System.out.println("我点到了哦！");
                Intent intent=new Intent(context, ManageAddressActivity.class);
                startActivity(intent);
                break;
        }
    }

    @OnClick(R.id.text_region) void selectAddr()
    {
        View view = LayoutInflater.from(context).inflate(R.layout.activity_edit_adress, null);
        setAddressSelectorPopup(view);
    }



    //不知道为啥，toolbar自带的点击方法在这里失效，只能用动态绑定的方法
    @OnClick(R.id.standard_toolbar_back) void backAddress() {
        System.out.println("我点到了哦！");
        Intent intent=new Intent(context, ManageAddressActivity.class);
        startActivity(intent);
    }



    @OnClick(R.id.button_save) void saveAddress() {
        Address address;
        if (mAddress == null) {
            address = new Address();
        } else {
            address = mAddress;
        }
        address.setReceiverName(mConsignee);
        address.setReceiverMobile(mTel);
        address.setReceiverMobile(mTel);

        address.setProvince(mProvinceName);
        address.setCity(mCityName);
        address.setArea(mDistrictName);
        address.setAddr(mDetail);
        address.setPostCode(mZipCode);
        int userid= PreferenceHelper.getInt("userId", Objects.requireNonNull(getContext()));
        if (mAddress == null) {
            // 添加新地址.
          //  address, userid, 0,receiverName,receiverMobile,province,city,area,addr,postCode;
            OkHttpClient client = new OkHttpClient();
            Request request1 = new Request.Builder()
                    .url(HttpUrl.getBaseUrl()+"/useraddr/insert/"+userid+"/"+0+"/"+address.getReceiverName()+"/"+
                            address.getReceiverMobile()+"/"+ address.getProvince()+"/"+address.getCity()+"/"+address.getArea()+"/"+address.getAddr()+"/"+address.getPostCode())
                    .addHeader("Token",token)
                    .build();
            client.newCall(request1).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    System.out.println(e.getMessage());
                }
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if(response.code() >= 200 && response.code() < 300) {
                        String results = response.body().string();
                        Result result=gson.fromJson(results,Result.class);
                        System.out.println("新的post请求"+result);
                    }
                }
            });
        } else {
            OkHttpClient client = new OkHttpClient();
            Request request1 = new Request.Builder()
                    .url(HttpUrl.getBaseUrl()+"/useraddr/insert/"+userid+"/"+mAddress.getAddrId()+"/"+address.getReceiverName()+"/"+
                            address.getReceiverMobile()+"/"+ address.getProvince()+"/"+address.getCity()+"/"+address.getArea()+"/"+address.getAddr()+"/"+address.getPostCode())
                    .addHeader("Token",token)
                    .build();
            client.newCall(request1).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    System.out.println(e.getMessage());
                }
                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if(response.code() >= 200 && response.code() < 300) {
                        String results = response.body().string();
                        Result result=gson.fromJson(results,Result.class);
                        System.out.println("新的post请求"+result);
                    }
                }
            });
        }
        Intent intent=new Intent(this,ManageAddressActivity.class);
        startActivity(intent);
    }

    private void checkAddressComplete() {
        if (TextUtils.isEmpty(mConsignee) || TextUtils.isEmpty(mTel) || TextUtils.isEmpty(mDetail)
                || TextUtils.isEmpty(mZipCode) || TextUtils.isEmpty(mEmail)) {
            btnSave.setEnabled(false);
            return;
        }
        btnSave.setEnabled(true);
    }

    private void clearRegionInfo() {
        mProvinceName = null;
        mCityName = null;
        mDistrictName = null;
        tvRegion.setText(null);
        btnSave.setEnabled(false);
    }

    private void setRegionText() {
        String info = String.format("%s - %s%s", mProvinceName, mCityName, mDistrictName);
        tvRegion.setText(info);
    }

    /**
     * 设置弹出PopWindow
     * @param v
     */
    private void setAddressSelectorPopup(View v) {
        int screenHeigh = getResources().getDisplayMetrics().heightPixels;

        CommonPopWindow.newBuilder()
                .setView(R.layout.pop_address_selector_bottom)
                .setAnimationStyle(R.style.AnimUp)
                .setBackgroundDrawable(new BitmapDrawable())
                .setSize(ViewGroup.LayoutParams.MATCH_PARENT, Math.round(screenHeigh * 0.6f))
                .setViewOnClickListener(this)
                .setBackgroundDarkEnable(true)
                .setBackgroundAlpha(0.7f)
                .setBackgroundDrawable(new ColorDrawable(999999))
                .build(getContext())
                .showAsBottom(v);
    }

    @Override
    public void getChildView(final PopupWindow mPopupWindow, View view, int mLayoutResId) {
        switch (mLayoutResId) {
            case R.layout.pop_address_selector_bottom:
                ImageView imageBtn = view.findViewById(R.id.img_guanbi);
                AddressSelector addressSelector = view.findViewById(R.id.address);

                //数据解析
                AddressSelectorReq addressSelectorReq = new Gson().fromJson(String.valueOf(response), AddressSelectorReq.class);
                //设置默认选择数据
                dealWithAddressSelector(addressSelector, addressSelectorReq.getDatas(), mPopupWindow);

                imageBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPopupWindow.dismiss();
                    }
                });

                break;
        }
    }

    private void dealWithAddressSelector(AddressSelector addressSelector, final List<AddressSelectorReq.DatasBean>
            addressSelectorList, final PopupWindow mPopupWindow) {
        final String[] sheng = new String[3];

        final ArrayList<ItemAddressReq> itemAddressReqs = getItemAddressSheng(addressSelectorList);
        addressSelector.setTabAmount(3);
        //设置数据
        addressSelector.setCities(itemAddressReqs);
        //设置Tab横线的颜色
        addressSelector.setLineColor(Color.parseColor("#D5A872"));
        //设置Tab文字默认颜色
        addressSelector.setTextEmptyColor(Color.parseColor("#000000"));
        //设置列表选中文字颜色
        addressSelector.setListTextSelectedColor(Color.parseColor("#D5A872"));
        //设置Tab文字选中的颜色
        addressSelector.setTextSelectedColor(Color.parseColor("#D5A872"));

        //设置列表的点击事件回调接口
        addressSelector.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void itemClick(AddressSelector addressSelector, CityInterface city, int tabPosition, int selecePos) {
                switch (tabPosition) {
                    case 0:
                        //设置省列表数据
                        sheng[0] = city.getCityName();
                        saveId[0] = addressSelectorList.get(selecePos).getDb_id();
                        childrenBeanXList = addressSelectorList.get(selecePos).getDb_children();
                        addressSelector.setCities(getItemAddressShi(childrenBeanXList));
                        break;
                    case 1:
                        //设置市列表数据
                        sheng[1] = city.getCityName();
                        saveId[1] = childrenBeanXList.get(selecePos).getCb_id();
                        childrenBeans = childrenBeanXList.get(selecePos).getCb_children();
                        addressSelector.setCities(getItemAddressQu(childrenBeans));
                        break;
                    case 2:
                        //设置区列表数据
                        sheng[2] = city.getCityName();
                        saveId[2] = childrenBeans.get(selecePos).getId();
                        tvRegion.setText(sheng[0] + sheng[1] + sheng[2]);//将获取到的完整地址写到对应的text框中
                        mProvinceName=sheng[0];//保存省份地址

                        mCityName=sheng[1];//保存城市地址
                        mDistrictName=sheng[2];//保存区域名称
                        mPopupWindow.dismiss();

                        break;
                }
            }
        });


        //设置顶部tab的点击事件回调
        addressSelector.setOnTabSelectedListener(new AddressSelector.OnTabSelectedListener() {
            @Override
            public void onTabSelected(AddressSelector addressSelector, AddressSelector.Tab tab) {
                switch (tab.getIndex()) {
                    case 0:
                        addressSelector.setCities(itemAddressReqs);
                        break;
                    case 1:
                        addressSelector.setCities(getItemAddressShi(childrenBeanXList));
                        break;
                    case 2:
                        addressSelector.setCities(getItemAddressQu(childrenBeans));
                        break;
                }
            }

            @Override
            public void onTabReselected(AddressSelector addressSelector, AddressSelector.Tab tab) {

            }
        });
    }


    /**
     * 获取省的数据
     *
     * @param addressSelectorList
     * @return
     */
    @NonNull
    private ArrayList<ItemAddressReq> getItemAddressSheng(List<AddressSelectorReq.DatasBean> addressSelectorList) {
        final ArrayList<ItemAddressReq> itemAddressReqs = new ArrayList<>();
        for (int i = 0; i < addressSelectorList.size(); i++) {
            ItemAddressReq itemAddressReq = new ItemAddressReq();
            itemAddressReq.setAreaAbbName(addressSelectorList.get(i).getDb_areaAbbName());
            itemAddressReq.setAreaCode(addressSelectorList.get(i).getDb_areaCode());
            itemAddressReq.setAreaEnName(addressSelectorList.get(i).getDb_areaEnName());
            itemAddressReq.setAreaType(addressSelectorList.get(i).getDb_areaType());
            itemAddressReq.setAreaZip(addressSelectorList.get(i).getDb_areaZip());
            itemAddressReq.setAreaName(addressSelectorList.get(i).getDb_areaName());
            itemAddressReq.setId(addressSelectorList.get(i).getDb_id());
            itemAddressReq.setParentId(addressSelectorList.get(i).getDb_parentId());
            itemAddressReqs.add(itemAddressReq);
        }
        return itemAddressReqs;
    }


    /**
     * 获取市的数据
     *
     * @return
     */
    @NonNull
    private ArrayList<ItemAddressReq> getItemAddressShi(List<AddressSelectorReq.DatasBean.ChildrenBeanX> datas) {
        final ArrayList<ItemAddressReq> itemAddressReqs = new ArrayList<>();
        for (int i = 0; i < datas.size(); i++) {
            ItemAddressReq itemAddressReq = new ItemAddressReq();
            itemAddressReq.setAreaAbbName(datas.get(i).getCb_areaAbbName());
            itemAddressReq.setAreaCode(datas.get(i).getCb_areaCode());
            itemAddressReq.setAreaEnName(datas.get(i).getCb_areaEnName());
            itemAddressReq.setAreaType(datas.get(i).getCb_areaType());
            itemAddressReq.setAreaZip(datas.get(i).getCb_areaZip());
            itemAddressReq.setAreaName(datas.get(i).getCb_areaName());
            itemAddressReq.setId(datas.get(i).getCb_id());
            itemAddressReq.setParentId(datas.get(i).getCb_parentId());
            itemAddressReqs.add(itemAddressReq);
        }
        return itemAddressReqs;
    }

    /**
     * 获取区的数据
     *
     * @param addressSelectorList
     * @return
     */
    @NonNull
    private ArrayList<ItemAddressReq> getItemAddressQu(List<AddressSelectorReq.DatasBean.ChildrenBeanX.ChildrenBean> addressSelectorList) {
        final ArrayList<ItemAddressReq> itemAddressReqs = new ArrayList<>();
        for (int i = 0; i < addressSelectorList.size(); i++) {
            ItemAddressReq itemAddressReq = new ItemAddressReq();
            itemAddressReq.setAreaAbbName(addressSelectorList.get(i).getAreaAbbName());
            itemAddressReq.setAreaCode(addressSelectorList.get(i).getAreaCode());
            itemAddressReq.setAreaEnName(addressSelectorList.get(i).getAreaEnName());
            itemAddressReq.setAreaType(addressSelectorList.get(i).getAreaType());
            itemAddressReq.setAreaZip(addressSelectorList.get(i).getAreaZip());
            itemAddressReq.setAreaName(addressSelectorList.get(i).getAreaName());
            itemAddressReq.setId(addressSelectorList.get(i).getId());
            itemAddressReq.setParentId(addressSelectorList.get(i).getParentId());
            itemAddressReqs.add(itemAddressReq);
        }
        return itemAddressReqs;
    }


}
